#include "look1_iu32lu8n8tu8_binlcase.h"
#include "div_nzp_repeat_u32.h"
#include "rtwtypes.h"

uint8_T look1_iu32lu8n8tu8_binlcase(uint32_T u0, const uint32_T bp0[], const
  uint8_T table[], uint32_T maxIndex)
{
  uint32_T iLeft;
  uint8_T frac;
  uint8_T y;
  if (u0 <= bp0[0U]) {
    iLeft = 0U;
    frac = 0U;
  } else if (u0 < bp0[maxIndex]) {
    uint32_T bpIdx;
    uint32_T iRght;
    bpIdx = maxIndex >> 1U;
    iLeft = 0U;
    iRght = maxIndex;
    while (iRght - iLeft > 1U) {
      if (u0 < bp0[bpIdx]) {
        iRght = bpIdx;
      } else {
        iLeft = bpIdx;
      }

      bpIdx = (iRght + iLeft) >> 1U;
    }

    bpIdx = bp0[iLeft];
    frac = (uint8_T)div_nzp_repeat_u32(u0 - bpIdx, bp0[iLeft + 1U] - bpIdx, 8U);
  } else {
    iLeft = maxIndex;
    frac = 0U;
  }

  if (iLeft == maxIndex) {
    y = table[iLeft];
  } else {
    uint8_T yL_0d0;
    uint8_T yR_0d0;
    yR_0d0 = table[iLeft + 1U];
    yL_0d0 = table[iLeft];
    if (yR_0d0 >= yL_0d0) {
      y = (uint8_T)((uint32_T)(uint8_T)(((uint32_T)(uint8_T)((uint32_T)yR_0d0 -
        yL_0d0) * frac) >> 8) + yL_0d0);
    } else {
      y = (uint8_T)((uint32_T)yL_0d0 - (uint8_T)(((uint32_T)(uint8_T)((uint32_T)
        yL_0d0 - yR_0d0) * frac) >> 8));
    }
  }

  return y;
}
